package com.book.lizhi.database;

import java.io.IOException;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

import com.android.volley.toolbox.ImageLoader.ImageCache;

public class BitmapCache implements ImageCache {

	private LruCache<String, Bitmap> mCache;

	public BitmapCache() {
		int maxSize = 10 * 1024 * 1024;
		mCache = new LruCache<String, Bitmap>(maxSize) {
			@Override
			protected int sizeOf(String key, Bitmap value) {
				return value.getRowBytes() * value.getHeight();
			}
		};
	}

	@Override
	public Bitmap getBitmap(String url) {
		if (mCache.get(url) == null) {
			return BitmapUtil.getBitmap(createKey(url));
		}
		return mCache.get(url);
	}

	@Override
	public void putBitmap(String url, Bitmap bitmap) {
		mCache.put(url, bitmap);
		try {
			BitmapUtil.saveBitmap(bitmap, createKey(url));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String createKey(String url) {
		return String.valueOf(url.hashCode());
	}

}